﻿using Microsoft.AspNetCore.Mvc;
using ServiceCore.Entity;
using ServiceCore.Entity.Arg;
using ServiceCore.Entity.Core;
using ServiceCore.Entity.Model;
using ServiceCore.Entity.View;
using ServiceCore.Service;
using ServiceCore.WebApi.jwt;

namespace ServiceCore.WebApi.Controllers
{
    /// <summary>
    /// 标签
    /// </summary>
    [Route("Api/[controller]/[action]")]
    [ApiController]
    public class RecordController : ApiJwtController
    {
        private RecordService recordService = new RecordService();
        private TextParseService textParseService = new TextParseService(); 

        /// <summary>
        /// 列表
        /// </summary>
        /// <arg name="arg"></arg>
        /// <returns></returns>
        [HttpPost]
        public BaseParam<RecordInfo, List<V_Record>> List(BaseParam<RecordInfo, List<V_Record>> arg)
        {
            arg = recordService.Record_List(arg);

            return arg;
        }

        /// <summary>
        /// 单个
        /// </summary>
        /// <arg name="arg"></arg>
        /// <returns></returns>
        [HttpPost]
        public BaseParam<RecordInfo, V_Record> GetModel(BaseParam<RecordInfo, V_Record> arg)
        {
            arg = recordService.Record_Exists(arg);

            return arg;
        }
        
        /// <summary>
        /// 新增
        /// </summary>
        /// <arg name="arg"></arg>
        /// <returns></returns>
        [HttpPost]
        public BaseParam<RecordInfo, long> Add(BaseParam<RecordInfo, long> arg)
        {
            var userInfo = GetJwtUserInfo();
            arg.Param.CreateUserId = userInfo.Id;
            arg = recordService.Record_Add(arg);

            return arg;
        }

        /// <summary>
        /// 编辑
        /// </summary>
        /// <arg name="arg"></arg>
        /// <returns></returns>
        [HttpPost]
        public BaseParam<RecordInfo, long> Edit(BaseParam<RecordInfo, long> arg)
        {
            arg = recordService.Record_Edit(arg);

            return arg;
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <arg name="arg"></arg>
        /// <returns></returns>
        [HttpPost]
        public BaseParam<RecordInfo, long> Del(BaseParam<RecordInfo, long> arg)
        {
            arg = recordService.Record_Delete(arg);

            return arg;
        }

        /// <summary>
        /// 清除所有
        /// </summary>
        /// <arg name="arg"></arg>
        /// <returns></returns>
        [HttpPost]
        public BaseParam<RecordInfo, long> Clear(BaseParam<RecordInfo, long> arg)
        {
            var user = GetJwtUserInfo();
            if (user == null)
            {
                arg.ErrorMsg = "无权限";
                return arg;
            }
            if(user.IsAdmin != 1)
            {
                arg.ErrorMsg = "无操作权限";
                return arg;
            }

            arg = recordService.Record_DeleteAll(arg);

            return arg;
        }

        /// <summary>
        /// 文字解析
        /// </summary>
        /// <arg name="arg"></arg>
        /// <returns></returns>
        [HttpPost]
        public ArgTextParse TextParse(ArgTextParse arg)
        {
            textParseService.Text_Parse(arg);

            return arg;
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <arg name="arg"></arg>
        /// <returns></returns>
        [HttpPost]
        public BaseParam<List<RecordInfo>, long> AddList(BaseParam<List<RecordInfo>, long> arg)
        {
            arg = recordService.Record_AddList(arg);

            return arg;
        }

    }
}
